help: ## Print documentation
	@{ grep -hE '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST); echo -e '$(EXTRA_HELP)'; } | sed 's/^ //' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-33s\033[0m %s\n", $$1, $$2}'

include lib.mk
include nix.mk

PROJECT_NAME = cardano-node
NUM_PROC     = $(nproc --all)

## One of:  shey alra mary alzo bage coay
ERA     ?= coay

PROFILE ?= default-${ERA}
BACKEND ?= supervisor
REV     ?= master
ITER    ?=
BATCH   ?=
ARGS    ?=
CMD     ?=
RUN     ?=

lint hlint: ## Run the CI version of hlint
	nix build --no-link '.#checks/hlint' --cores 0
haddock-hoogle haddocks hoogle:
	if test -z "$$IN_NIX_SHELL"; then nix-shell --run 'cabal update && cabal haddock all --haddock-hoogle'; else cabal update && cabal haddock all --haddock-hoogle; fi
host-hlint: ## Run the system (not Nix) version of hlint
	hlint bench cardano-{api,cli,node,node-capi,node-chairman,submit-api,testnet,tracer}

stylish-haskell: ## Apply stylish-haskell on all *.hs files
	@find . -type f -name "*.hs" -not -path '.git' -print0 | xargs -0 stylish-haskell -i

cabal-hashes:
	nix run .#checkCabalProject

cli node:
	cabal --ghc-options="+RTS -qn8 -A32M -RTS" build cardano-$@

trace-documentation:
	cabal run -- exe:cardano-node trace-documentation --config 'configuration/cardano/mainnet-config.yaml' --output-file 'doc/new-tracing/tracers_doc_generated.md'

###
### Workbench
###
workbench-ci: workbench-ci-test ci-test-auto ci-test-autonix
CI_TARGETS := hlint workbench-ci haddock-hoogle
ci:  ci-report ci-targets
ci-report:
	@echo -e "\033[34mGoals under test\033[0m:  \033[33m$(CI_TARGETS)\033[0m"
ci-targets:  $(CI_TARGETS)

workbench-internals-walkthrough:
	emn nix/workbench/doc.org

##
## Base targets:
##
shell:                                           ## Nix shell, (workbench from /nix/store), vars: PROFILE, CMD, RUN
	nix-shell -A 'workbench-shell' --max-jobs 8 --cores 0 --show-trace --argstr profileName ${PROFILE} --argstr backendName ${BACKEND} ${ARGS} ${if ${CMD},--command "${CMD}"} ${if ${RUN},--run "${RUN}"}
shell-dev shell-prof shell-nix: shell
shell-nix: ARGS += --arg 'useCabalRun' false ## Nix shell, (workbench from Nix store), vars: PROFILE, CMD, RUN
shell-prof: ARGS += --arg 'profiling' '"space"'  ## Nix shell, everything Haskell built profiled

analyse: RUN := wb analyse std ${TAG}
analyse: shell

list-profiles:                                   ## List workbench profiles
	nix build .#all-profiles-json && cat result
show-profile:                                    ## NAME=profile-name
	@test -n "${NAME}" || { echo 'HELP:  to specify profile to show, add NAME=profle-name' && exit 1; }
	nix build .#all-profiles-json --json --option substitute false | jq '.[0].outputs.out' -r | xargs jq ".\"${NAME}\" | if . == null then error(\"\n###\n### Error:  unknown profile: ${NAME}  Please consult:  make list-profiles\n###\") else . end"
ps:                                              ## Plain-text list of profiles
	@nix build .#workbench.profile-names-json --json | jq '.[0].outputs.out' -r | xargs jq '.[]' --raw-output

##
## Profile-based cluster shells (autogenerated targets)
##
## wb_profiles.mk is autogenerated by `cardano-profile lib-make`. It contains all known profile families and flavours (minus the era, backend and shell type suffixes).
## After adding or removing profile definitions in `cardano-profile`, you'll probably want to also commit a regenerated version of that file.
##
include wb_profiles.mk

$(eval $(call define_profile_targets,           $(LOCAL_PROFILES)))
$(eval $(call define_profile_targets_nomadcloud,$(CLOUD_PROFILES)))

# Dynamic local/supervisor profile targets.
playground-%:
	nix-shell -A 'workbench-shell' --max-jobs 8 --cores 0 --show-trace --argstr profileName $*-${ERA} --argstr backendName supervisor

###
### Misc
###
clean-profile proclean:
	rm -f *.html *.prof *.hp *.stats *.eventlog

clean: clean-profile
	rm -rf logs/ socket/ cluster.*

full-clean: clean
	rm -rf db dist-newstyle $(shell find . -name '*~' -or -name '*.swp')

cls:
	echo -en "\ec"

.PHONY: cabal-hashes clean cli cls cluster-profiles help node run-test shell shell-dev stylish-haskell $(LOCAL_PROFILES) workbench-ci-test
